package com.android.ccarvajal.ejemplos;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.GridView;
import android.widget.Toast;
//import android.widget.Toast;

public class GrillaCategorias extends Activity {
	/** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
    	super.onCreate(savedInstanceState);
    	setContentView(R.layout.catalog);
    	
    	GridView Categorias = (GridView)findViewById(R.id.gridcatalog);
    	Categorias.setAdapter(new AdaptadorImagenCatalogo(this));
    	
    	Categorias.setOnItemClickListener(new OnItemClickListener(){
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
				onGridItemClick(arg0, arg1, arg2, arg3);
			}
		});
    }
    
    protected class AdaptadorImagenCatalogo extends BaseAdapter {
    	
    	private Context mContext;
    	
    	public AdaptadorImagenCatalogo(Context c){
    		mContext = c;
    	}

    	public int getCount() {
    		// TODO Auto-generated method stub
    		return mThumbIds.length;
    	}

    	public Object getItem(int position) {
    		// TODO Auto-generated method stub
    		return null;
    	}

    	public long getItemId(int position) {
    		// TODO Auto-generated method stub
    		return 0;
    	}

    	public View getView(int position, View convertView, ViewGroup parent) {
    		// TODO Auto-generated method stub
    		ImageView imageView;
    		if (convertView == null) {  // if it's not recycled, initialize some attributes
                imageView = new ImageView(mContext);
                imageView.setLayoutParams(new GridView.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
                imageView.setScaleType(ImageView.ScaleType.FIT_CENTER);
                imageView.setPadding(8, 8, 8, 8);
            } else {
                imageView = (ImageView) convertView;
            }

            imageView.setImageResource(mThumbIds[position]);
            return imageView;
    	}
    	
    	 // references to our images
        private Integer[] mThumbIds = {
        		R.drawable.categoria_craneomxf
        		,R.drawable.categoria_deportiva
        		,R.drawable.categoria_general
        		,R.drawable.categoria_spine
        		
        };

    }

    
    public void onGridItemClick(AdapterView<?> parent, View vista, int position, long id) {
        //Toast.makeText(GrillaCategorias.this, "" + position, Toast.LENGTH_SHORT).show();
    	Intent formulario;
		if(position==2)
		{
			try {
				formulario = new Intent().setClass(GrillaCategorias.this, GaleriaProductosGeneral.class);
				startActivity(formulario);
			} catch (Exception e) {
				// TODO: handle exception
				Toast.makeText(GrillaCategorias.this, e.toString(), Toast.LENGTH_SHORT).show();
			}
		}
    }

}
